package com.ydj.app.service;

import com.ydj.app.entity.CmsAccount;
import com.ydj.app.entity.Kol;
import com.ydj.app.entity.WebUser;
import com.ydj.app.entity.dao.KolDao;
import com.ydj.app.entity.dao.WebUserDao;
import com.ydj.app.entity.query.KolQuery;
import com.ydj.app.entity.query.WebUserQuery;
import com.ydj.app.protocol.http.cms.CmsWebUserListReq;
import com.ydj.app.protocol.http.cms.CmsWebUserListRes;
import com.ydj.app.protocol.http.cms.CmsWebUserUpdateLevelReq;
import com.ydj.framework.exception.CommonException;
import com.ydj.framework.mysql.PageArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * Web用户相关服务
 */
@Slf4j
@Service
public class WebUserService {
    @Autowired
    private KolDao kolDao;
    @Autowired
    private WebUserDao webUserDao;

    /**
     * 列表
     */
    public CmsWebUserListRes cmsList(CmsWebUserListReq req, CmsAccount account) {
        PageArgs pageArgs = req.getPageArgs();

        List<WebUser> list = new WebUserQuery(webUserDao)
                .nickNameLike(req.getKeyword())
                .level(req.getMemberLevel())
                .page(pageArgs);

        CmsWebUserListRes res = new CmsWebUserListRes(pageArgs);
        for (WebUser user : list) {
            CmsWebUserListRes.CmsWebUserListResItem item = new CmsWebUserListRes.CmsWebUserListResItem()
                    .setId(user.getId())
                    .setNickName(user.getNickName())
                    .setBindWeChat(user.getOpenId() != null)
                    .setMemberLevel(user.getLevel())
                    .setExpireTime(user.getExpTime())
                    .setRemark(user.getRemark());
            res.getList().add(item);
        }
        return res;
    }

    /**
     * 更改会员等级
     */
    @Transactional(rollbackOn = Exception.class)
    public void cmsUpdateLevel(CmsWebUserUpdateLevelReq req, CmsAccount account) throws CommonException {
        WebUser user = new WebUserQuery(webUserDao)
                .id(req.getId())
                .single();
        CommonException.throwExceptionIfNull(user, "用户不存在");
        if (req.getMemberLevel().isMember() && req.getExpTime() == null) {
            CommonException.throwException("请填写有效期");
        }

        if (req.getMemberLevel() == user.getLevel()) {
            return;
        }

        // 从非会员升级为会员, 增加推荐人的人数了
        if (!user.getLevel().isMember() && req.getMemberLevel().isMember()) {
            user.setBecomeMember(true);
            // 推荐人
            if (user.getKolId() != null && !user.getBecomeMember()) {
                Kol kol = new KolQuery(kolDao)
                        .id(user.getKolId())
                        .single();
                if (kol != null) {
                    kol.incInvite();
                    kolDao.saveOrUpdate(kol);
                }
            }
        }

        user.setLevel(req.getMemberLevel());
        if (user.getLevel().isMember()) {
            user.setExpTime(req.getExpTime());
        } else {
            user.setExpTime(null);
        }
        webUserDao.saveOrUpdate(user);

    }
}